package com.zhudeshuai.zhudemolist.widget.MyScrollView;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.LinearSnapHelper;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.view.View;

/**
 * @author zhudeshuai
 * @since 17/12/15
 */

public class MySnapHelper extends LinearSnapHelper {

	private OrientationHelper mHorizontalHelper;

	@Nullable
	@Override
	public int[] calculateDistanceToFinalSnap(RecyclerView.LayoutManager layoutManager, View targetView) {
		int[] out = new int[2];
		if (layoutManager.canScrollHorizontally()) {
			out[0] = distanceToStart(targetView, getHorizontalHelper(layoutManager));
		} else {
			out[0] = 0;
		}

		return out;
	}

	private int distanceToStart(View targetView, OrientationHelper helper) {
		return helper.getDecoratedStart(targetView) - helper.getStartAfterPadding();
	}

	@Nullable
	@Override
	public View findSnapView(RecyclerView.LayoutManager layoutManager) {
		return findStartView(layoutManager, getHorizontalHelper(layoutManager));
	}

	private View findStartView(RecyclerView.LayoutManager layoutManager,
							   OrientationHelper helper) {

		if (layoutManager instanceof LinearLayoutManager) {
			int firstChild = ((LinearLayoutManager) layoutManager).findFirstVisibleItemPosition();
			int lastChild = ((LinearLayoutManager) layoutManager).findLastVisibleItemPosition();
			if (firstChild == RecyclerView.NO_POSITION) {
				return null;
			}
			if (lastChild == layoutManager.getItemCount() - 1) {
				return layoutManager.findViewByPosition(lastChild);
			}

			View child = layoutManager.findViewByPosition(firstChild);

			if (helper.getDecoratedEnd(child) >= helper.getDecoratedMeasurement(child) / 2
					&& helper.getDecoratedEnd(child) > 0) {
				return child;
			} else {
				return layoutManager.findViewByPosition(firstChild + 1);
			}
		}

		return super.findSnapView(layoutManager);
	}


	private OrientationHelper getHorizontalHelper(
			@NonNull RecyclerView.LayoutManager layoutManager) {
		if (mHorizontalHelper == null) {
			mHorizontalHelper = OrientationHelper.createHorizontalHelper(layoutManager);
		}
		return mHorizontalHelper;
	}
}
